//
//  ModifyPwdViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/11.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "ModifyPwdViewController.h"
#import "BkInputItemView.h"
#import "RegularExpression.h"
@interface ModifyPwdViewController ()
@property (nonatomic,strong) BkInputItemView *oldPswView;
@property (nonatomic,strong) BkInputItemView *nePswView;
@property (nonatomic,strong) UIButton *commitBtn;
@end

@implementation ModifyPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改登录密码";
    [self.view addSubview:self.oldPswView];
    [self.view addSubview:self.nePswView];
    [self.view addSubview:self.commitBtn];
}

-(BkInputItemView *)oldPswView{
    if (!_oldPswView) {
        _oldPswView = [[BkInputItemView alloc]initWithTop:TSScaleW(20) Title:@"旧密码" placeHolder:@"请输入旧密码"];
        _oldPswView.inputTextField.secureTextEntry = YES;
    }
    return _oldPswView;
}
-(BkInputItemView *)nePswView{
    if (!_nePswView) {
        _nePswView = [[BkInputItemView alloc]initWithTop:TSScaleW(10) + _oldPswView.bottom Title:@"新密码" placeHolder:@"请设置新密码"];
        _nePswView.inputTextField.secureTextEntry = YES;
    }
    return _nePswView;
}

-(UIButton *)commitBtn{
    if (!_commitBtn) {
        _commitBtn = [TSWLTools allocButtonTitle:@"确定" font:systemFont(TSScaleW(15)) textColor:kWhiteColor image:nil frame:CGRectMake(TSScaleW(30), _nePswView.bottom + TSScaleW(50), TSScaleW(316), TSScaleW(40)) sel:@selector(commitAction:) taget:self];
        _commitBtn.backgroundColor = kGreenColor;
        [_commitBtn setCornerRadius:_commitBtn.height/2.f];
    }
    return _commitBtn;
}

-(void)commitAction:(UIButton *)sender{
     NSString *oldPassWord = [TSWLTools md5:_oldPswView.inputTextField.text];
    NSString *passWord = [TSWLTools md5:_nePswView.inputTextField.text];
    if ([self pamasIsOkOrNot]) {
        NSDictionary *pamas = @{@"oldpassword":oldPassWord,@"password":passWord};
        WS(weakSelf);
           [NetWorkTools postConJSONWithUrl:@"updatePassword" parameters:pamas success:^(id responseObject) {
               
               NSString *result = responseObject[@"result"];
               NSString *resultNote = responseObject[@"resultNote"];
               if ([result integerValue] == 0) {
                   [weakSelf.navigationController popViewControllerAnimated:YES];
               }
               [MBProgressHUD showError:resultNote];
              } fail:^(NSError *error) {
                  
        }];
    }
   
}

-(BOOL)pamasIsOkOrNot{
   
       if (!_oldPswView.inputTextField.text.length) {
          // [SSTool error:SSKJLanguage(@"请输入您的手机号")];
           [MBProgressHUD showError:@"请输入您的原密码"];
           return NO;
       }

       if (!_nePswView.inputTextField.text.length) {
           [MBProgressHUD showError:SSKJLanguage(@"请输入设置新密码")];
          return NO;
       }
       if (![RegularExpression validatePassword:self.nePswView.inputTextField.text]) {
           [MBProgressHUD showError:SSKJLanguage(@"密码格式不正确")];
          return NO;
       }
       
    return YES;
}
@end
